package cn.sll.pet.service.impl;

import cn.sll.base.query.BaseQuery;
import cn.sll.base.util.PageList;
import cn.sll.org.domain.Employee;
import cn.sll.org.vo.EmpAndRoleVo;
import cn.sll.pet.domain.PetType;
import cn.sll.pet.mapper.PetTypeMapper;
import cn.sll.pet.service.IPetTypeService;
import cn.sll.base.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sll
 * @since 2023-01-08
 */
@Service
public class PetTypeServiceImpl extends BaseServiceImpl<PetType> implements IPetTypeService {
    
    @Autowired
    private PetTypeMapper petTypeMapper;

    @Override
    public List<PetType> tree() {
        List<PetType> result = new ArrayList<>();
        //查询所有数据
        List<PetType> all = petTypeMapper.findAll();
        //将全部数据转换成map key:id value:对象
        Map<Long, PetType> petTypeMap = all.stream().collect(Collectors.toMap(PetType::getId, type -> type));
        //在内存中组装数据
        all.stream().forEach(type->{
            if(null == type.getPid()){
                //一级数据
                result.add(type);
            }else{
                PetType parent = petTypeMap.get(type.getPid());
                parent.getChildren().add(type);
            }
        });
        return result;
    }

    @Override
    public PageList<PetType> queryByPage(BaseQuery query) {
        PageList<PetType> pageList = super.queryByPage(query);
        if (null == pageList || CollectionUtils.isEmpty(pageList.getRows())){
            return pageList;
        }
        // 我们要干的事：现在要将 员工对应的 角色id集合 添加到对象中
        // 根据empID查询到所有的role集合
        // 先去获取到所有的empid集合
        List<PetType> petTypeList = pageList.getRows();
        List<Long> pidList = petTypeList.stream().map(PetType::getId).collect(Collectors.toList());
        // 根据 empIdList 去中间表中 查询 所有的roleId 集合

        // 需要对vos 进行分组 返回一个map map的key：empId map的value：List<Long> roleIds

        return pageList;
    }
}
